<html>
<head>

<title>Groovy Goodness: Use Categories to Add Functionality to Classes</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Use Categories to Add Functionality to Classes</h3>

<div class="post">
<p><a href="http://groovy.codehaus.org/Groovy+Categories">Categories</a> allow us to add extra functionality to classes, even those classes we didn't develop ourselves. A <code>Category</code> class contain static methods. The first argument of the method determines the type the method can be applied to.</p><p>We can also use the <code>@Category</code> transformation annotation to make a class into a <code>Category</code>. The class doesn't have to define static methods anymore, but we can use instance methods. The parameter of the <code>@Category</code> annotation defines for which class the <code>Category</code> is.</p><p>The <code>Category</code> can be applied to a specific code block with the <code>use</code> keyword. Only within the code block the rules of the <code>Category</code> are applied.</p>
<pre class="brush:groovy">
class Speak {
    static String shout(String text) {  // Method argument is String, so we can add shout() to String object.
        text.toUpperCase() + '!'
    }
    
    static String whisper(String text, boolean veryQuiet = false) {
        "${veryQuiet ? 'sssssssh' : 'sssh'}.. $text"
    }
    
    static String army(String text) {
        "$text. Sir, yes sir!"
    }
}

use (Speak) {
    assert 'PAY ATTENTION!' == "Pay attention".shout()
    assert 'sssh.. Be vewy, vewy, quiet.' == "Be vewy, vewy, quiet.".whisper()
    assert 'sssssssh.. Be vewy, vewy, quiet.' == "Be vewy, vewy, quiet.".whisper(true)
    assert 'Groovy rocks. Sir, yes sir!' == "Groovy rocks".army()
}

// Or we can use the @Category annotation.
@Category(String) 
class StreetTalk {
    String hiphop() {
        "Yo, yo, here we go. ${this}"
    }
}

use(StreetTalk) {
    assert 'Yo, yo, here we go. Groovy is fun!' == 'Groovy is fun!'.hiphop()
}

// We can use static methods from other Java classes.
use (org.apache.commons.codec.digest.DigestUtils) {
    assert '900150983cd24fb0d6963f7d28e17f72' == 'abc'.md5Hex()
}
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>